package com.rongzhe.house.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.AppCompatImageView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.presenter.HouseSearchPresenter;
import com.rongzhe.house.presenter.PageContoller;
import com.rongzhe.house.ui.adapter.HouseAdapter;
import com.rongzhe.house.ui.fragment.FilterDateFragment;
import com.rongzhe.house.ui.fragment.FilterRentTypeFragment;
import com.rongzhe.house.ui.fragment.OtherFilterFragment;
import com.rongzhe.house.ui.fragment.PositionFilterFragment;
import com.rongzhe.house.ui.view.smoothlist.SmoothListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnEditorAction;

/**
 * Created by zhongyanli on 2017/11/12.
 */

public class HouseSearchActivity extends BaseActivity<HouseSearchPresenter>
        implements PageContoller.PageControlInteface<HouseOverview> {
    @BindView(R.id.btn_search)
    EditText btnSearch;
    @BindView(R.id.btn_filter_1)
    CheckBox btnFilter1;
    @BindView(R.id.btn_filter_2)
    CheckBox btnFilter2;
    @BindView(R.id.btn_filter_3)
    CheckBox btnFilter3;
    @BindView(R.id.btn_filter_4)
    CheckBox btnFilter4;
    @BindView(R.id.container_filter)
    LinearLayout containerFilter;
    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.list_data)
    SmoothListView listData;
    @BindView(R.id.btn_back)
    AppCompatImageView btnBack;
    @BindView(R.id.empty_view)
    TextView emptyView;

    private HouseAdapter mAdapter;

    private FilterDateFragment mFilterDateFragment;
    private FilterRentTypeFragment mFilterRentTypeFragment;
    private OtherFilterFragment mOtherFilterFragment;
    private PositionFilterFragment mPositionFilterFragment;
    private Fragment mCurrentShowingFragment = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        mAdapter = new HouseAdapter(this);
        initView();
        mFilterDateFragment = new FilterDateFragment();
        mFilterDateFragment.setFilterChangeListener(mPresenter);
        mFilterRentTypeFragment = new FilterRentTypeFragment();
        mFilterRentTypeFragment.setFilterChangeListener(mPresenter);
        mOtherFilterFragment = new OtherFilterFragment();
        mOtherFilterFragment.setFilterChangeListener(mPresenter);
        mPositionFilterFragment = new PositionFilterFragment();
        mPositionFilterFragment.setFilterChangeListener(mPresenter);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame_content, mFilterDateFragment)
                .add(R.id.frame_content, mFilterRentTypeFragment)
                .add(R.id.frame_content, mOtherFilterFragment)
                .add(R.id.frame_content, mPositionFilterFragment)
                .commit();
    }

    @OnEditorAction(R.id.btn_search)
    public boolean onSearchClick(int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            String keyWords = btnSearch.getText().toString();
            mPresenter.setKeyWords(keyWords);
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (mCurrentShowingFragment != null) {
            showFragment(null);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        showFragment(null);
    }

    @Override
    protected void onRequestPermissGranted(boolean granted, String permission) {
        if(mCurrentShowingFragment.equals(mPositionFilterFragment)) {
            ((PositionFilterFragment)mCurrentShowingFragment).onRequestPermissGranted(granted,
                    permission);
        }
    }

    @SuppressLint("RestrictedApi")
    private void showFragment(Fragment showingFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragmentTransaction.hide(fragment);
        }
        if (showingFragment != null) {
            fragmentTransaction.show(showingFragment);
            frameContent.setVisibility(View.VISIBLE);
        } else {
            frameContent.setVisibility(View.GONE);
            mCurrentShowingFragment = null;
        }
        fragmentTransaction.commit();
        setCheckboxStatus();
    }

    private void setCheckboxStatus() {
        btnFilter1.setChecked(HouseManager.getInstance().hasTypeFilter());
        btnFilter2.setChecked(HouseManager.getInstance().hasPositionFilter());
        btnFilter3.setChecked(HouseManager.getInstance().hasTimeFilter());
        btnFilter4.setChecked(HouseManager.getInstance().hasRentFilter());
    }

    @Override
    protected boolean hideLeftActionItem() {
        return false;
    }

    @Override
    protected HouseSearchPresenter createPresenter() {
        return new HouseSearchPresenter(this);
    }

    @OnClick({R.id.btn_filter_1, R.id.btn_filter_2, R.id.btn_filter_3, R.id.btn_filter_4})
    public void onFilterChanged(View view) {
        int id = view.getId();
        Fragment fragment = null;
        switch (id) {
            case R.id.btn_filter_1:
                fragment = mFilterRentTypeFragment;
                break;
            case R.id.btn_filter_2:
                fragment = mPositionFilterFragment;
                break;
            case R.id.btn_filter_3:
                fragment = mFilterDateFragment;
                break;
            case R.id.btn_filter_4:
                fragment = mOtherFilterFragment;
                break;
            default:
                break;
        }
        if (fragment != null) {
            if (fragment.equals(mCurrentShowingFragment)) {
                showFragment(null);
            } else {
                showFragment(fragment);
                mCurrentShowingFragment = fragment;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HouseManager.getInstance().clearAllFilter();
    }

    public void resetToTop() {
        listData.smoothScrollToPosition(0);
    }

    @OnClick(R.id.btn_back)
    public void onBackClick() {
        super.onBackPressed();
    }

    @Override
    public void addData(List<HouseOverview> houseInfos) {
        mAdapter.addHouseInfos(houseInfos);
    }

    @Override
    public void setAsNoData(boolean noData) {
            emptyView.setVisibility(noData ? View.VISIBLE : View.GONE);
    }
    @Override
    public void setData(List<HouseOverview> houseInfos) {
        mAdapter.setHouseInfos(houseInfos);
    }

    @Override
    public void loadData(boolean append) {
        mPresenter.loadData(append);
    }

    @Override
    public SmoothListView getListView() {
        return listData;
    }

    private void initView() {
        listData.setAdapter(mAdapter);
    }

    @Override
    protected boolean showActionBar() {
        return false;
    }
}
